﻿/***********************************************************************
 * <copyright file="ValidateRequired.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Thursday, October 17, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

namespace BusinessObjects.BusinessRules
{
    /// <summary>
    ///     Represents a validation rules that states that a value is required.
    /// </summary>
    public class ValidateRequired : BusinessRule
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="ValidateRequired" /> class.
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        public ValidateRequired(string propertyName)
            : base(propertyName)
        {
            ErrorMessage = propertyName + " is a required field.";
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="ValidateRequired" /> class.
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        /// <param name="errorMessage">The error message.</param>
        public ValidateRequired(string propertyName, string errorMessage)
            : base(propertyName)
        {
            ErrorMessage = errorMessage;
        }

        /// <summary>
        ///     Validation method. To be implemented in derived classes.
        /// </summary>
        /// <param name="businessObject">The business object.</param>
        /// <returns>
        ///     Boolean value
        /// </returns>
        public override bool Validate(BusinessObject businessObject)
        {
            try
            {
                return GetPropertyValue(businessObject).ToString().Length > 0;
            }
            catch
            {
                return false;
            }
        }
    }
}